
package com.universest.swordholder.ui.screen

import android.content.Intent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowForwardIos
import androidx.compose.material.icons.filled.Info
import androidx.compose.material.icons.filled.Person
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material.icons.filled.Style
import androidx.compose.material.icons.filled.SwitchAccount
import androidx.compose.material.icons.filled.TipsAndUpdates
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.universest.swordholder.SwordHolderApplication
import com.universest.swordholder.model.user.UserManager
import com.universest.swordholder.ui.theme.SwordHolderTheme
import androidx.compose.runtime.getValue
import androidx.compose.ui.res.stringResource
import com.universest.swordholder.R
import com.universest.swordholder.ui.activity.ConfigActivity

@Composable
fun MeScreen(modifier: Modifier = Modifier) {
    val scrollState = rememberScrollState()
    val user by UserManager.instance.currentUserFlow.collectAsStateWithLifecycle()
    val context = LocalContext.current
    Column(
        modifier = modifier
            .verticalScroll(scrollState),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Spacer(modifier = Modifier.height(32.dp))

        // 圆形头像
        Box(
            modifier = Modifier
                .size(100.dp)
                .clip(CircleShape)
                .background(MaterialTheme.colorScheme.onPrimary)
        ){
            Icon(
                modifier = Modifier.fillMaxSize().padding(10.dp),
                imageVector = Icons.Default.Person,
                contentDescription = null,
                tint = MaterialTheme.colorScheme.primary
            )
        }

        Spacer(modifier = Modifier.height(16.dp))

        // 名字
        Text(
            color = MaterialTheme.colorScheme.onSurfaceVariant,
            text = "${user.nickName} [${user.userName}]",
            style = MaterialTheme.typography.headlineSmall
        )

        Spacer(modifier = Modifier.height(32.dp))

        // 配置按钮
        /*
        ConfigurationButton(
            text = stringResource(id = R.string.switch_account),
            icon = Icons.Default.SwitchAccount
        )
        */

        ConfigurationButton(
            text = stringResource(id = R.string.app_settings),
            icon = Icons.Default.Settings,
            onClick = {
                val intent = Intent(context, ConfigActivity::class.java)
                context.startActivity(intent)
            }
        )
        /*
        ConfigurationButton(
            text = stringResource(id = R.string.prompt_settings),
            icon = Icons.Default.TipsAndUpdates
        )
        ConfigurationButton(
            text = stringResource(id = R.string.custom_features),
            icon = Icons.Default.Style
        )
        ConfigurationButton(
            text = stringResource(id = R.string.about_app),
            icon = Icons.Default.Info
        )*/
    }
}

@Composable
fun ConfigurationButton(
    text: String,
    icon: ImageVector,
    modifier: Modifier = Modifier,
    onClick: () -> Unit = {}
) {
    TextButton(
        onClick = onClick,
        modifier = modifier
            .fillMaxWidth()
            .height(52.dp),
        colors = ButtonDefaults.textButtonColors(
            containerColor = Color.Transparent
        ),
        contentPadding = PaddingValues(0.dp),
        shape = RectangleShape
    ) {
        Row(
            modifier = Modifier.fillMaxSize(),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.SpaceBetween
        ) {
            Row(
                verticalAlignment = Alignment.CenterVertically,
                modifier = Modifier.padding(start = 15.dp)
            ) {
                Icon(
                    imageVector = icon,
                    contentDescription = null,
                    modifier = Modifier.size(24.dp),
                    tint = MaterialTheme.colorScheme.onSurfaceVariant
                )
                Spacer(modifier = Modifier.width(16.dp))
                Text(
                    text = text,
                    fontSize = 18.sp,
                    color = MaterialTheme.colorScheme.onSurfaceVariant
                )
            }
            Icon(
                imageVector = Icons.AutoMirrored.Filled.ArrowForwardIos,
                contentDescription = null,
                modifier = Modifier.padding(end = 16.dp)
                    .size(15.dp),
                tint = MaterialTheme.colorScheme.onSurfaceVariant
            )
        }
    }
}

@Composable
@Preview
fun MeScreenPreview() {
    SwordHolderApplication.previewContext = LocalContext.current
    SwordHolderTheme {
        MeScreen(modifier = Modifier.fillMaxSize())
    }
}